<?php
class HomeAction extends StudyBaseAction
{
	/**
	 *
	 */
	public function homeList()
	{
		$center = M('Home');
		import('ORG.Util.Page');
		$count = $center->count();// 查询满足要求的总记录数
		$page = new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $center->limit($limit)->order("id desc")->select();
		$show = $page->show();
		$this->assign('page',$show);
		$this->assign('list',$list);
		$p = $_GET['p'];
		if(empty($p)){
			$p = 1;
		}
		$this->assign('p',$p);
		$this->display('Home:homeList');
	}
	/**
	 * 查看
	 */
	public function homeLook()
	{
		$form = M('Home');
		$where["id"] = $_GET['id'];
		$list = $form->where($where)->find();
		$game = M("Home_image");
		$home_image["home_id"] = $_GET['id'];
		$homeImage = $game->where($home_image)->select();
		$this->assign("list",$list);
		$this->assign("homeImage",$homeImage);
		$this->display();
	}
	/**
	 * 编辑页面
	 */
	public function homeEditView()
	{
		$form = M('Home');
		$where["id"] = $_GET['id'];
		$list = $form->where($where)->find();
		$game = M("Home_image");
		$home_image["home_id"] = $_GET['id'];
		$homeImage = $game->where($home_image)->select();
		$this->assign("list",$list);
		$this->assign("homeImage",$homeImage);
		$this->assign('p',$_GET['p']);
		$this->display();
	}
	/**
	 * 删除
	 */
	public function homeDel()
	{
		$home = M("Home"); // 实例化Center对象
		$home_image = M('Home_image');
		$id['id']=$_GET['id'];
		$home_id['home_id']=$_GET['id'];
		$home_image->where($home_id)->delete(); // 删除图片
		$home->where($id)->delete(); // 删除机构
		//$_SESSION['message'] = '删除成功！';
		$p = $_GET['p'];
		$this->redirect('Home:homeList?p='.$p);
	}
	/**
	 * 新增页面
	 */
	public function homeAddView()
	{
		$this->display();
	}
	/**
	 * 新增
	 */
	public function homeAdd()
	{
		$curr_time = date('Y-m-d H:i',time());
		//新增机构
		$center = M('Home');
		$center->create();
		$center->create_time=$curr_time;
		$id = $center->add();
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/home/';// 设置附件上传目录
		//$upload->thumb=true;//是否开启图片文件缩略
		//$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		//$upload->thumbMaxHeight='400,100';
		//$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		// 保存表单数据 包括附件数据
		$game = M("Home_image");
		$game->create();
		for($i=0;$i<count($info);$i++){
			$game->image = $info[$i]['savename'];   //等同与$img=array('img'=>$data[0]['savename']);
			$game->home_id = $id;
			$game->add();
		}
		$this->redirect('Home:homeList');
	}
	/**
	 * 提交修改
	 */
	public function homeEdit()
	{
		//修改机构
		$home = M('Home');
		$save['title'] = $_POST['title'];
		$where['id'] = $_POST['id'];
		$home->where($where)->data($save)->save();
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/home/';// 设置附件上传目录
		//$upload->thumb=true;//是否开启图片文件缩略
		//$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		//$upload->thumbMaxHeight='400,100';
		//$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		// 保存表单数据 包括附件数据
		$game = M("Home_image");
		//$game->create();
		for($i=0;$i<count($info);$i++){
			$game->image = $info[$i]['savename'];   //等同与$img=array('img'=>$data[0]['savename']);
			$game->home_id = $_POST['id'];
			$game->add();
		}
		$this->redirect('Home:homeList');
	}
	/**
	 * 编辑时删除图片
	 */
	public function delImg()
	{
		$game = M("Home_image");
		$where['id']=$_POST['id'];
		$game->where($where)->delete(); // 删除
		$this->ajaxReturn("","JSON");
	}
	/**
	 * 首页显示
	 */
	public function homeHome()
	{
		$game = M("Home");
		$wheres['type']='0';
		$dataHome['home'] = '0';
		$ww = $game->data($dataHome)->where($wheres)->save();
		$where['id']=$_GET['id'];
		$data['home'] = '1';
		$game->data($data)->where($where)->save();
		$this->redirect('Home:homeList');
	}
}
?>